﻿using UnityEngine;
using System.Collections;
using Assets.Game.scripts.ui.models;
using System.Linq;
using System.Collections.Generic;
using Engine.Entities.Character;
using Assets.Game.scripts.components;

namespace Assets.Game.scripts.ui.controllers {

    [RequireComponent(typeof(PartyItemRendererModel))]
    public class PartyCompositionWidgetController : MonoBehaviour {


        public UIGrid charactersInPartyGrid;
        public UIGrid charactersWithoutPartyGrid;
        public UIScrollView charactersInPartyGridScrollView;
        public UIScrollView charactersWithoutPartyGridScrollView;
        public GameObject characterInPartyItemRenderer;
        public GameObject characterWithoutPartyItemRenderer;
        private bool isRedrawRequired;

        void Start() {
            PartyItemRendererModel model = this.GetComponent<PartyItemRendererModel>();

            if (model != null && this.charactersInPartyGrid != null && this.charactersWithoutPartyGrid != null) {
                //aggiungo i personaggi senza comitiva
                if (this.characterWithoutPartyItemRenderer != null) {
                    IEnumerable<SteveBrain> characters =
                        from SteveBrain brain in FrenzyPeopleController.singleton.SteveList
                        where brain.party == null
                        select brain;

                    foreach (SteveBrain aloneCharacter in characters) {
                        SteveModel agentModel = aloneCharacter.model;
                        GameObject itemRendererInstance = NGUITools.AddChild(
                            this.charactersWithoutPartyGrid.gameObject,
                            this.characterWithoutPartyItemRenderer
                        );

                        CharacterItemRendererModel itemModel = itemRendererInstance.GetComponent<CharacterItemRendererModel>();
                        if (itemModel != null) {
                            itemModel.data = aloneCharacter;
                        }

                        UIDragDropItemListener dragListener = itemRendererInstance.GetComponent<UIDragDropItemListener>();
                        EventDelegate assignToPartyEndCallback = new EventDelegate(this, "onDragEnd");
                        assignToPartyEndCallback.parameters[0].value = itemRendererInstance;
                        assignToPartyEndCallback.parameters[1].value = itemModel;
                        if (dragListener != null) {
                            EventDelegate.Set(dragListener.onDragEnd, assignToPartyEndCallback);
                        }
                    }
                }

                //aggiungo i personaggi della comitiva
                if (this.characterInPartyItemRenderer != null) {
                    foreach (SteveBrain character in model.party.PeopleInParty) {
                        SteveModel agentModel = character.model;
                        GameObject itemRendererInstance = NGUITools.AddChild(
                            this.charactersInPartyGrid.gameObject,
                            this.characterInPartyItemRenderer
                        );

                        CharacterItemRendererModel itemModel = itemRendererInstance.GetComponent<CharacterItemRendererModel>();
                        if (itemModel != null) {
                            itemModel.data = character;
                        }

                        UIDragDropItemListener dragListener = itemRendererInstance.GetComponent<UIDragDropItemListener>();
                        EventDelegate removeFromPartyEndCallback = new EventDelegate(this, "onDragEnd");
                        removeFromPartyEndCallback.parameters[0].value = itemRendererInstance;
                        removeFromPartyEndCallback.parameters[1].value = itemModel;
                        if (dragListener != null) {
                            EventDelegate.Set(dragListener.onDragEnd, removeFromPartyEndCallback);
                        }
                    }
                }
            }

            this.isRedrawRequired = true;
        }

        void Update() {
            if (this.isRedrawRequired) {
                if (this.charactersInPartyGrid != null && this.charactersInPartyGridScrollView != null) {
                    this.charactersInPartyGridScrollView.ResetPosition();
                    this.charactersInPartyGridScrollView.UpdatePosition();
                    this.charactersInPartyGrid.Reposition();
                }

                if (this.charactersWithoutPartyGrid != null && this.charactersWithoutPartyGridScrollView != null) {
                    this.charactersWithoutPartyGridScrollView.ResetPosition();
                    this.charactersWithoutPartyGridScrollView.UpdatePosition();
                    this.charactersWithoutPartyGrid.Reposition();
                }

                this.isRedrawRequired = false;
            }
        }

        public void onClose() {
            NGUITools.Destroy(this.gameObject);
        }

        public void onDragEnd(GameObject draggedItem, CharacterItemRendererModel item) {
            Transform parent = draggedItem.transform.parent;
            bool isDraggedOnPartyGrid = System.Object.ReferenceEquals(parent, this.charactersInPartyGrid.transform);
            bool isDraggedOnPeopleGrid = System.Object.ReferenceEquals(parent, this.charactersWithoutPartyGrid.transform);

            if (parent.gameObject != null) {
                PartyItemRendererModel model = this.GetComponent<PartyItemRendererModel>();
                if (isDraggedOnPartyGrid) {
                    //aggiungo l'individuo alla comitiva
                    FrenzyPeopleController.instance.AddInParty(item.data, model.party);
                } else if (isDraggedOnPeopleGrid) {
                    //rimuovo l'individuo dalla comitiva
                    FrenzyPeopleController.instance.RemoveFromParty(item.data);
                }
            }
        }
    }
}